<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>
	<body>
	</body>
	<script>
		//解构赋值:只要等号左右两边的模式相同，左边的变量就会被赋予对应的值
		console.log('解构赋值');
		let [x,y,z] = [1,2,3];
		console.log(x,y,z);
		let [,a,b] = [0,01,02];
		console.log(a,b);
		let [head,,...tail] = [1,2,3,4,5,6,7];
		console.log(head,tail);
		let [o,p,...q] = ['a'];
		console.log(o,p,q);
		let [m,n] = ['m','n','p'];
		console.log(m,n);
		/*let [foo,[[bar],baz]] = [1,[[2],3]];
		console.log(foo,bar,baz);*/
		//会报错的解构赋值如下
		/*let [foo] = 1;
		let [foo] = false;
		let [foo] = NaN;
		let [foo] = undefined;
		let [foo] = null;
		let [foo] = {};
		console.log(foo);*/
		function* fibs() {
			let a = 0;
			let b = 1;
			while(true){
				yield a;
				[a,b] = [b,(a+b)*2];
			}
		}
		let [first,second,third,fourth,fifth,sixth] = fibs();
		console.log(sixth);
		//解构赋值允许指定默认值,除非后面的赋值位置的值严格等于undefined，默认值才会有效
		let [x1,y1='b'] = ['a',undefined];
		console.log(x1,y1);
		let [x2,y2='b'] = ['a','bb'];
		console.log(x2,y2);
		let [x3='x3'] = [];
		console.log(x3);
		/*let [x4='x4'] = {};*///报错
		//console.log(x3);
		
		function xx5(){
			return 'x5';
		}
		let [x5 = xx5()] = [];
		console.log(x5);
		let [xx,yy = xx] = [1,undefined];
		console.log(xx,yy);
		//对象的解构赋值
		let {ab,ac} = {ab:'ab',ac:'ac'};
		console.log(ab,ac);
		let {foo,foo:baz} = {foo:'foo',bar:'bar'};
		console.log(foo,baz);
		//字符串的解构赋值
		const {length : len} = 'hello world!';
		console.log(len);
		const [aa,bb,cc,...dd] = 'hello world!';
		console.log(aa,bb,cc,dd);
		let arr1 = [[1,2],[3,4]].map(([x,y]) => x*y);
		console.log(arr1);
		//运用 for of 用于对象
		const map1 = new Map();
		map1.set('first','hello');
		map1.set('second','world');
		for(let [key,value] of map1){
			console.log(key + ' is ' + value);
		}
		for(let [key] of map1){
			console.log('only key ' + key);
		}
		for(let [,value] of map1){
			console.log('only value ' + value);
		}
		//运用 for of 用于数组
		let arr11 = ['a','b','c'];
		for(let index of arr11.keys()){
			console.log(index);
		}
		for(let item of arr11){
			console.log(item);
		}
		//console.log(Array.prototype.values());
		for(let [a,b] of arr11.entries()){
			console.log(a,b);
		}
	</script>
</html>
